package org.leumasjaffe.initiativeTracker.distributed.controller;

import static org.leumasjaffe.initiativeTracker.operator.InitiativeController.tracker;

import org.eclipse.jdt.annotation.NonNullByDefault;
import org.leumasjaffe.initiativeTracker.entity.CharacterList;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;

import edu.wpi.heineman.xml.Message;
import edu.wpi.heineman.client.IMessageHandler;

@NonNullByDefault
public class RollHandler implements IMessageHandler {

	@SuppressWarnings("null")
	@Override
	public void process(Message response) {
		NodeList nl = response.contents.getFirstChild().getChildNodes();
		CharacterList list = tracker(response.contents.getFirstChild().getAttributes()
				.getNamedItem("game").getNodeValue()).list;

		for (int i = 0; i < nl.getLength(); i++) {
			NamedNodeMap map = nl.item(0).getAttributes();
			
			int id = Integer.decode(map.getNamedItem("id").getNodeValue());
			int init = Integer.decode(map.getNamedItem("value").getNodeValue());
			
			list.lookup(id).getCharacter().setInit(init);
		}
		
	}

	@Override
	public IMessageHandler newCopy() {
		return new RollHandler();
	}

}
